package com.jubao.ejb.business.impl;

import java.util.List;

import javax.ejb.Stateless;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.business.Business;
import com.jubao.ejb.business.ProductBusinessLocal;
import com.jubao.ejb.business.ProductBusinessRemote;
import com.jubao.ejb.business.util.BusinessConstant;
import com.jubao.ejb.entity.Brand;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.GeneRelation;
import com.jubao.ejb.entity.Product;
import com.jubao.ejb.entity.ProductPrice;
import com.jubao.ejb.entity.Store;
import com.jubao.ejb.entity.User;

@Stateless
@LocalBinding(jndiBinding = BusinessConstant.ContentBusinessLocalJndiName)
@RemoteBinding(jndiBinding = BusinessConstant.ContentBusinessRemoteJndiName)
public class ProductBusinessImpl extends Business implements ProductBusinessLocal, ProductBusinessRemote {

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.ProductBusiness#getProducts(long, long, int, int)
	 */
	public List<Product> getProductList(long productCategorySysId, long brandSysId, int pgno, int pgsize) {
		int relationId1 = productCategorySysId > -1 ? BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_CATEGORY.getId()
				: -1;
		int relationId2 = brandSysId > -1 ? BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_BRAND.getId() : -1;

		List products = super.geneRelationDao.getEntityList(Product.class.getName(), relationId1, productCategorySysId,
				relationId2, brandSysId, pgno, pgsize);
		return products;
	}

	public long getProductCount(long productCategorySysId, long brandSysId) {
		int relationId1 = productCategorySysId > -1 ? BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_CATEGORY.getId()
				: -1;
		int relationId2 = brandSysId > -1 ? BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_BRAND.getId() : -1;

		Long result = super.geneRelationDao.getEntityCount(Product.class.getName(), relationId1, productCategorySysId,
				relationId2, brandSysId);
		return result;
	}

	public List<ProductPrice> getProductPriceList(long areaCategorySysId, long storeSysId, long productSysId, int pgno,
			int pgsize) {
		int relationAreaId = -1;
		if (areaCategorySysId > -1)
			relationAreaId = BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId();
		return productPriceDao.getProductPriceList(relationAreaId, areaCategorySysId, storeSysId,
				BusinessConstant.RelationID.STORE_IS_SELLER_OF_PRODUCT.getId(), productSysId, pgno, pgsize);
	}

	public long getProductPriceCount(long areaCategorySysId, long storeSysId, long productSysId) {
		int relationAreaId = -1;
		if (areaCategorySysId > -1)
			relationAreaId = BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId();
		return productPriceDao.getProductPriceCount(relationAreaId, areaCategorySysId, storeSysId,
				BusinessConstant.RelationID.STORE_IS_SELLER_OF_PRODUCT.getId(), productSysId);
	}

	public ProductPrice findProductPrice(long storeSysId, long productSysId) {
		List<ProductPrice> pps = this.getProductPriceList(-1, storeSysId, productSysId, 0, 1);
		if (pps.size() > 0)
			return pps.get(0);
		return null;
	}

	public ProductPrice saveProductPrice(long storeSysId, long productSysId, ProductPrice productPrice) {
		Store store = storeDao.find(storeSysId);
		Product product = productDao.find(productSysId);
		if (store != null && product != null && productPrice != null) {
			productPrice.setStore(store);
			productPrice.setRelationId(BusinessConstant.RelationID.STORE_IS_SELLER_OF_PRODUCT.getId());
			productPrice.setProduct(product);
			return productPriceDao.save(productPrice);
		}
		return null;
	}

	public void add2Brand(long productSysId, long brandSysId) {
		Product product = productDao.find(productSysId);
		Brand brand = brandDao.find(brandSysId);
		if (product != null && brand != null) {
			Brand oldBrand = product.getBrand();
			if (oldBrand != null) {
				GeneRelation oldgr = geneRelationDao.find(productSysId,
						BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_BRAND.getId(), oldBrand.getSysId());
				geneRelationDao.remove(oldgr);				
			}
			product.setBrand(brand);
			GeneRelation gr = geneRelationDao.find(productSysId,
					BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_BRAND.getId(), brand.getSysId());
			if(gr== null){
				gr = new GeneRelation();
				gr.setEntity(product);
				gr.setRelationId(BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_BRAND.getId());
				gr.setEntity2(brand);
				geneRelationDao.save(gr);
			}			
		}
	}

	public void add2CateogryTrail(long productSysId, long categorySysId) {
		Product product = productDao.find(productSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (product != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(productSysId,
						BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr == null) {
					gr = new GeneRelation();
					gr.setEntity(product);
					gr.setRelationId(BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_CATEGORY.getId());
					gr.setEntity2(category);
					geneRelationDao.save(gr);
				}
			}
		}
	}

	public void removeFromCategoryTrail(long productSysId, long categorySysId) {
		Product product = productDao.find(productSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (product != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(productSysId,
						BusinessConstant.RelationID.PRODUCT_IS_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr!= null) {
					geneRelationDao.remove(gr);
				}
			}
		}
	}


}
